#include <dde_kit/lock.h>

#include "conf.h"

#include <eku/stdlib.h>
#include <inc/x86_lock.h>

struct dde_kit_lock
{
	 spin_lock_t lock;
};

void
dde_kit_lock_init(struct dde_kit_lock **out_lock)
{
	 struct dde_kit_lock *result;
	 *out_lock =
		  result = (struct dde_kit_lock *)
		  malloc(sizeof(struct dde_kit_lock));
	 spl_init(&result->lock);
}

void
dde_kit_lock_deinit(struct dde_kit_lock *lock)
{
	 free(lock);
}

void
dde_kit_lock_lock(struct dde_kit_lock *lock)
{
	 spl_acquire(&lock->lock);
}

int
dde_kit_lock_try_lock(struct dde_kit_lock *lock)
{
	 int result = spl_try_acquire(&lock->lock);
	 if (result)
	 {
		  print_backtrace();
	 }
	 return result;
}

void
dde_kit_lock_unlock(struct dde_kit_lock *lock)
{
	 spl_release(&lock->lock);
}

#include <eku/symbol.h>

void
dde_kit_lock_symbol(void)
{
	 export_symbol("dde_kit_lock_init", &dde_kit_lock_init, 1);
	 export_symbol("dde_kit_lock_deinit", &dde_kit_lock_deinit, 1);
	 export_symbol("dde_kit_lock_lock", &dde_kit_lock_lock, 1);
	 export_symbol("dde_kit_lock_try_lock", &dde_kit_lock_try_lock, 1);
	 export_symbol("dde_kit_lock_unlock", &dde_kit_lock_unlock, 1);
}
